layui.use(['layer','table','form'], function(args){
    layer = layui.layer;
    form = layui.form;
    laydate = layui.laydate;
    //监听表单提交，这样才能触发表单验证
    form.on('submit(process-properties-from)', function(data){
        var loadIndex = layer.load(2);
        //通过表单完成任务
        $.ajax({
            url: basePath+'/process/task/completeByFormData.do',
            data: {properties: JSON.stringify(data.field),id:data.field.id},//这里把表单数据转成json字符串
            dataType:'json',
            type:'post',
            success: function (data, textStatus) {
                layer.close(loadIndex);
                layer.alert(data.message,function(index){
                    if (data.success) {
                        location.reload();
                    }
                    layer.close(index);
                });
            }
        });
        //不通过默认的方式提交表单
        return false;
    });
});

/**
 * 完成任务
 * 先查询任务是否有表单，如果有则弹出表单，并通过表单数据完成任务，否则直接完成任务
 * @param id 任务ID
 */
function completeTask(id) {
    //弹出加载层
    var loadIndex = layer.load(2);
    $.ajax({
        url: basePath+'process/form/getTaskFormHtml.do',
        data: {taskId: id},
        dataType: 'text',
        success: function (data, textStatus) {
            //关闭加载层
            layer.close(loadIndex);
            if (data && data!='' && data.length!=0){//有表单
                //打开表单窗口
                layer.open({
                    title:"填写流程表单",
                    type: 1,
                    maxmin:true,
                    area:['600px','450px'],
                    content: '<form id="processPropertiesForm" action=""  class="layui-form" style="padding: 10px;"><input type="hidden" name="id" value="'+id+'"/>'+data+'<div class="layui-form-item">' +
                    '        <div class="layui-input-block">' +
                    '            <button class="layui-btn" lay-submit lay-filter="process-properties-from">提交</button>' +
                    '            <button type="reset" class="layui-btn layui-btn-primary">重置</button>' +
                    '        </div>' +
                    '    </div></form>'
                });
                //渲染下拉框和日期控件
                form.render();
                laydate.render({
                    elem:'#processPropertiesForm input.date'
                    ,type:'datetime'
                    ,format: 'yyyy-MM-dd HH:mm'
                    ,min: '2017-8-11 08:30:00'
                    ,max: '2017-12-30 17:30:00'
                });
            }else{//没有表单，直接完成任务
                var loadIndex2 = layer.load(2);
                $.ajax({
                    type:'POST',
                    url:basePath+'process/task/complete.do',
                    data: {id:id},
                    dataType: 'json',
                    success: function (data, textStatus) {
                        layer.close(loadIndex2);
                        layer.alert(data.message,function (index) {
                            layer.close(index);
                            if (data.success) {
                                location.reload();
                            }
                        });
                    }
                })
            }

        }
    });

}